import os.path

import requests
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget

import global_config
from ui.Change_password_window import Change_password_window
from window.My_pop_up_warning import My_pop_up_warning


class My_change_password_window(QWidget, Change_password_window):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        base_path = os.path.dirname(__file__)
        base_path = base_path.replace('\\window', '')
        icon_path = os.path.join(base_path, 'icon')
        icon_path = os.path.join(icon_path, 'Python.png')

        # 设置窗口标题和图标
        self.setWindowTitle('修改密码')
        icon = QIcon(icon_path)
        self.setWindowIcon(icon)

        self.username = ''
        self.password = ''

        self.pop_up_warning = My_pop_up_warning()

        self.input_current_password.setPlaceholderText('请输入当前密码：')
        self.input_new_password.setPlaceholderText('请输入新密码：')
        self.input_confirm_new_password.setPlaceholderText('请确认密码：')
        self.input_current_password_wrong.setText('')
        self.input_new_password_wrong.setText('')
        self.input_confirm_new_password_wrong.setText('')

        # 修改密码界面的确认修改和取消按钮
        self.confirm_button.clicked.connect(self.attempt_to_change_password)
        self.cancel_button.clicked.connect(self.close)

        self.input_current_password.textChanged.connect(self.update_input_current_password_wrong)
        self.input_new_password.textChanged.connect(self.update_input_new_password_wrong)
        self.input_confirm_new_password.textChanged.connect(self.update_input_confirm_new_password_wrong)


    def update_input_current_password_wrong(self):
        if self.input_current_password.text() == '':
            self.input_current_password_wrong.setText('密码不能为空')
        else:
            self.input_current_password_wrong.setText('')

    def update_input_new_password_wrong(self):
        if self.input_new_password.text() == '':
            self.input_new_password_wrong.setText('密码不能为空')
        elif self.check_password(self.input_new_password.text()):
            self.input_new_password_wrong.setText('密码应包含大小写字母和数字')
        elif self.input_new_password.text() == self.input_current_password.text():
            self.input_new_password_wrong.setText('不可以与旧密码一致')
        else:
            self.input_new_password_wrong.setText('')

    def update_input_confirm_new_password_wrong(self):
        if self.input_confirm_new_password.text() == '':
            self.input_confirm_new_password_wrong.setText('密码不能为空')
        elif self.input_confirm_new_password.text() != self.input_new_password.text():
            self.input_confirm_new_password_wrong.setText('两次输入的密码不一致')
        else:
            self.input_confirm_new_password_wrong.setText('')

    def set_username(self, username):
        self.username = username

    def set_password(self, password):
        self.password = password

    def attempt_to_change_password(self):
        is_empty = False
        is_error = False
        if self.input_current_password.text() == '':
            is_empty = True
            self.input_current_password_wrong.setText('密码不能为空')
            self.input_current_password_wrong.setVisible(True)

        if self.input_new_password.text() == '':
            is_empty = True
            self.input_new_password_wrong.setText('密码不能为空')
            self.input_new_password_wrong.setVisible(True)

        if self.input_confirm_new_password.text() == '':
            is_empty = True
            self.input_confirm_new_password_wrong.setText('密码不能为空')
            self.input_confirm_new_password_wrong.setVisible(True)

        if not is_empty:
            if self.input_current_password.text() != self.password:
                is_error = True
                self.input_current_password_wrong.setText('旧密码错误')
                self.input_current_password_wrong.setVisible(True)

            if self.input_new_password.text() != self.input_confirm_new_password.text():
                is_error = True
                self.input_new_password_wrong.setText('两次密码输入不一致')
                self.input_new_password_wrong.setVisible(True)
                self.input_confirm_new_password_wrong.setText('两次密码输入不一致')
                self.input_confirm_new_password_wrong.setVisible(True)

            if self.input_new_password.text() == self.password:
                is_error = True
                self.input_new_password_wrong.setText('不可以与旧密码一致')
                self.input_new_password_wrong.setVisible(True)

            if self.check_password(self.input_new_password.text()):
                is_error = True
                self.input_new_password_wrong.setText('密码应包含大小写字母和数字')

        if not is_empty and not is_error:
            url = 'http://' + global_config.localhost + ':8000/change_password/'
            params = {
                'username': self.username,
                'password': self.password,
                'new_pw': self.input_new_password.text()
            }
            result = requests.get(url, params=params).json()
            if result['result'] == 'success':
                # 将登录后的界面的密码修改为新密码
                self.password = self.input_new_password.text()
                # 将修改密码界面的输入信息清空
                self.input_current_password.setText('')
                self.input_new_password.setText('')
                self.input_confirm_new_password.setText('')

                self.close()
            elif result['result'] == 'mismatch':
                self.pop_up_warning.warning_info_label.setText('修改密码失败')
                self.pop_up_warning.warning_info_text.setText('旧密码错误')
                self.pop_up_warning.show()
            elif result['result'] == 'empty':
                self.pop_up_warning.warning_info_label.setText('修改密码失败')
                self.pop_up_warning.warning_info_text.setText('新密码为空')
                self.pop_up_warning.show()

    def check_password(self, password):
        result1 = False
        result2 = False
        result3 = False
        for i in password:
            if '0' <= i <= '9':
                result1 = True
            elif 'a' <= i <= 'z':
                result2 = True
            elif 'A' <= i <= 'Z':
                result3 = True
        return not (result1 and result2 and result3)

    # 重写关闭，将警示信息设为不可见，并清空输入栏中的内容
    def closeEvent(self, event):
        self.input_current_password.setText('')
        self.input_new_password.setText('')
        self.input_confirm_new_password.setText('')

